package com.itbaizhan;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**获取数据库连接的优化*/

public class JdbcTest01 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        //实例化Properties对象
        Properties prop = new Properties();
        //获取properties文件输入流对象
        /*注意路径：要在src下创建properties*/
        InputStream inp = JdbcTest01.class.getClassLoader().getResourceAsStream("jdbc.properties");
        //load()方法解释并读取properties文件
        prop.load(inp);
        //获取数据
        String url = prop.getProperty("url");
        String name = prop.getProperty("username");
        String pwd = prop.getProperty("pwd");
       //获取数据驱动器全名
        String drivername = prop.getProperty("driver");
        //加载并注册驱动
        Class.forName(drivername);
        //通过驱动管理器对象获取连接对象
       Connection connection = DriverManager.getConnection(url,name,pwd);
        System.out.println(connection);
    }
}
